package com.xydtech.workflow.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.workflow.domain.WflFlowstep;
import com.xydtech.workflow.domain.WflStepDoc;
import com.xydtech.workflow.service.IWflFlowstepService;
import com.xydtech.workflow.service.IWflStepDocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 流程节点影像资料配置Controller
 *
 * @author JHON
 * @date 2022-01-04
 */
@Controller
@RequestMapping("/workflow/stepDoc")
public class WflStepDocController extends BaseController {
    private String prefix = "workflow/stepDoc";

    @Autowired
    private IWflStepDocService wflStepDocService;
    @Autowired
    private IWflFlowstepService wflFlowstepService;

    /**
     * 新增保存流程节点影像资料配置
     */
    @Log(title = "流程节点影像资料配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Long versionId, Long orderNum, String docIds) {
        //根据版本号和序号查询流程节点信息
        WflFlowstep wflFlowstep = wflFlowstepService.selectWflFlowstepByVersionIdAndOrderNum(versionId, orderNum);
        //查询已配置页面信息
        WflStepDoc oldWflStepDoc = new WflStepDoc();
        oldWflStepDoc.setVersionId(versionId);
        oldWflStepDoc.setStepId(wflFlowstep.getStepId());
        List<WflStepDoc> oldDocList = wflStepDocService.selectWflStepDocList(oldWflStepDoc);
        //加工新增资料信息
        List<WflStepDoc> addDocList = new ArrayList<WflStepDoc>();
        String[] docs = docIds.split(",");
        for (int i = 0; i < docs.length; i++) {
            WflStepDoc newWflStepDoc = new WflStepDoc();
            newWflStepDoc.setVersionId(versionId);
            newWflStepDoc.setStepId(wflFlowstep.getStepId());
            newWflStepDoc.setDocId(Long.parseLong(docs[i]));
            //默认非必传
            newWflStepDoc.setIsMust("F");
            //序号在原有基础上递增
            newWflStepDoc.setOrderNum((long) (oldDocList.size() + i + 1));
            addDocList.add(newWflStepDoc);
        }
        return toAjax(wflStepDocService.insertWflStepDoc(addDocList));
    }

    /**
     * 修改保存流程节点影像资料配置
     */
    @Log(title = "流程节点影像资料配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WflStepDoc wflStepDoc) {
        return toAjax(wflStepDocService.updateWflStepDoc(wflStepDoc));
    }

    /**
     * 删除流程节点影像资料配置
     */
    @Log(title = "流程节点影像资料配置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long versionId, Long stepId, Long docId) {
        WflStepDoc wflStepDoc = new WflStepDoc();
        wflStepDoc.setDocId(docId);
        wflStepDoc.setStepId(stepId);
        wflStepDoc.setVersionId(versionId);
        return toAjax(wflStepDocService.deleteWflStepDoc(wflStepDoc));
    }
}
